home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11950 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sprintf() question
  5. Date: Wed, 27 Mar 96 21:07:55 GMT
  6. Organization: none
  7. Message-ID: <827960875snz@genesis.demon.co.uk>
  8. References: <31593522.76B3@cbm.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <31593522.76B3@cbm.com> paynedc@cbm.com "Dave Payne" writes:
  15.  
  16. >Question on sprintf():
  17. >
  18. >Is it safe, and more importantly, is it ANSI standard, to use sprintf()
  19. >to print into the same variable?  Consider this example:
  20. >
  21. >char foo[100] = "foobar";
  22. >
  23. >sprintf(foo,"%s%s","the string is",foo);
  24.  
  25. The standard says:
  26.  
  27. 7.9.6.5  The sprintf function
  28.  
  29. "If copying takes place between objects that overlap, the behaviour is
  30.  undefined"
  31.  
  32. So this code is illegal.
  33.  
  34. >Would the resulting string be "the string is foobar", or will this
  35. >code cause problems because I'm using the same variable (foo) to
  36. >print to as well as read from?
  37.  
  38. Anything at all can happen.
  39.  
  40. -- 
  41. -----------------------------------------
  42. Lawrence Kirby | fred@genesis.demon.co.uk
  43. Wilts, England | 70734.126@compuserve.com
  44. -----------------------------------------
  45.